package com.langyiquan.service.impl;

import com.langyiquan.config.WXPayResource;
import com.langyiquan.entity.WxPreOrder;
import com.langyiquan.entity.WxPreOrderResult;
import com.langyiquan.exception.http.PayException;
import com.langyiquan.service.WxPayService;
import com.langyiquan.utils.HttpUtils;
import com.langyiquan.utils.WxPaySign;
import com.langyiquan.utils.XmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

@Service
public class WxPayServiceImpl implements WxPayService {

    @Autowired
    private WXPayResource wxPayResource;

    @Override
    public WxPreOrderResult placeOrder(String body, String out_trade_no, String total_fee) {

        // 生成预付单对象
        WxPreOrder o = new WxPreOrder();
        // 生成随机字符串
        String nonce_str = UUID.randomUUID().toString().trim().replaceAll("-", "");
        o.setAppid(wxPayResource.getAppId());
        o.setBody(body);
        o.setMch_id(wxPayResource.getMerchantId());
        o.setNotify_url(wxPayResource.getNotifyUrl());
        o.setOut_trade_no(out_trade_no);
        // 判断有没有输入订单总金额，没有输入默认1分钱
        if (total_fee != null && !total_fee.equals("")) {
            o.setTotal_fee(Integer.parseInt(total_fee));
        } else {
            o.setTotal_fee(1);
        }
        o.setNonce_str(nonce_str);
        o.setTrade_type(wxPayResource.getTradeType());
        o.setSpbill_create_ip(wxPayResource.getSpbillCreateIp());
        SortedMap<Object, Object> p = new TreeMap<Object, Object>();
        p.put("appid", wxPayResource.getAppId());
        p.put("mch_id", wxPayResource.getMerchantId());
        p.put("body", body);
        p.put("nonce_str", nonce_str);
        p.put("out_trade_no", out_trade_no);
        p.put("total_fee", total_fee);
        p.put("spbill_create_ip", wxPayResource.getSpbillCreateIp());
        p.put("notify_url", wxPayResource.getNotifyUrl());
        p.put("trade_type", wxPayResource.getTradeType());
        // 获得签名
        String sign = WxPaySign.createSign("utf-8", p, wxPayResource.getSecrectKey());
        o.setSign(sign);
        // Object转换为XML
        String xml = null;
        try {
            xml = XmlUtil.object2Xml(o, WxPreOrder.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 统一下单地址
        String url = wxPayResource.getPlaceOrderUrl();
        // 调用微信统一下单地址
        String returnXml = HttpUtils.doPost(url, xml);

        // XML转换为Object
        try {
            return (WxPreOrderResult) XmlUtil.xml2Object(returnXml, WxPreOrderResult.class);
        } catch (Exception e) {
            throw new PayException(7001);
        }

    }


    @Override
    public WxPreOrderResult placeOrder(String body, String out_trade_no, String total_fee,String openid) {

        // 生成预付单对象
        WxPreOrder o = new WxPreOrder();
        // 生成随机字符串
        String nonce_str = UUID.randomUUID().toString().trim().replaceAll("-", "");
        o.setAppid(wxPayResource.getAppId());
        o.setBody(body);
        o.setMch_id(wxPayResource.getMerchantId());
        o.setNotify_url(wxPayResource.getNotifyUrl());
        o.setOut_trade_no(out_trade_no);
        o.setOpenid(openid);
        // 判断有没有输入订单总金额，没有输入默认1分钱
        if (total_fee != null && !total_fee.equals("")) {
            o.setTotal_fee(Integer.parseInt(total_fee));
        } else {
            o.setTotal_fee(1);
        }
        o.setNonce_str(nonce_str);
        o.setTrade_type(wxPayResource.getTradeType());
        o.setSpbill_create_ip(wxPayResource.getSpbillCreateIp());
        SortedMap<Object, Object> p = new TreeMap<Object, Object>();
        p.put("appid", wxPayResource.getAppId());
        p.put("mch_id", wxPayResource.getMerchantId());
        p.put("body", body);
        p.put("openid",openid);
        p.put("nonce_str", nonce_str);
        p.put("out_trade_no", out_trade_no);
        p.put("total_fee", total_fee);
        p.put("spbill_create_ip", wxPayResource.getSpbillCreateIp());
        p.put("notify_url", wxPayResource.getNotifyUrl());
        p.put("trade_type", wxPayResource.getTradeType());
        // 获得签名
        String sign = WxPaySign.createSign("utf-8", p, wxPayResource.getSecrectKey());
        o.setSign(sign);
        // Object转换为XML
        String xml = null;
        try {
            xml = XmlUtil.object2Xml(o, WxPreOrder.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 统一下单地址
        String url = wxPayResource.getPlaceOrderUrl();
        // 调用微信统一下单地址
        String returnXml = HttpUtils.doPost(url, xml);

        // XML转换为Object
        try {
            return (WxPreOrderResult) XmlUtil.xml2Object(returnXml, WxPreOrderResult.class);
        } catch (Exception e) {
            throw new PayException(7001);
        }

    }
}
